{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Print Words Vertically"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: printVertically"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #竖直打印单词"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串&nbsp;<code>s</code>。请你按照单词在 <code>s</code> 中的出现顺序将它们全部竖直返回。<br>\n",
    "单词应该以字符串列表的形式返回，必要时用空格补位，但输出尾部的空格需要删除（不允许尾随空格）。<br>\n",
    "每个单词只能放在一列上，每一列中也只能有一个单词。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;HOW ARE YOU&quot;\n",
    "<strong>输出：</strong>[&quot;HAY&quot;,&quot;ORO&quot;,&quot;WEU&quot;]\n",
    "<strong>解释：</strong>每个单词都应该竖直打印。 \n",
    " &quot;HAY&quot;\n",
    "&nbsp;&quot;ORO&quot;\n",
    "&nbsp;&quot;WEU&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;TO BE OR NOT TO BE&quot;\n",
    "<strong>输出：</strong>[&quot;TBONTB&quot;,&quot;OEROOE&quot;,&quot;   T&quot;]\n",
    "<strong>解释：</strong>题目允许使用空格补位，但不允许输出末尾出现空格。\n",
    "&quot;TBONTB&quot;\n",
    "&quot;OEROOE&quot;\n",
    "&quot;   T&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s = &quot;CONTEST IS COMING&quot;\n",
    "<strong>输出：</strong>[&quot;CIC&quot;,&quot;OSO&quot;,&quot;N M&quot;,&quot;T I&quot;,&quot;E N&quot;,&quot;S G&quot;,&quot;T&quot;]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 200</code></li>\n",
    "\t<li><code>s</code>&nbsp;仅含大写英文字母。</li>\n",
    "\t<li>题目数据保证两个单词之间只有一个空格。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [print-words-vertically](https://leetcode.cn/problems/print-words-vertically/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [print-words-vertically](https://leetcode.cn/problems/print-words-vertically/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"HOW ARE YOU\"', '\"TO BE OR NOT TO BE\"', '\"CONTEST IS COMING\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = list(s.split(' '))\n",
    "        x = max(len(word) for word in words)\n",
    "        res = []\n",
    "        i = 0\n",
    "        for i in range(x):\n",
    "            concat = ''.join([word[i] if i < len(word) else \" \" for word in words])\n",
    "            res.append(concat.rstrip())\n",
    "        return res\n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split(' ')\n",
    "        maxLen = max(len(word) for word in words)\n",
    "        res = []\n",
    "        for i in range(maxLen):\n",
    "            tmp = \"\".join([word[i] if i < len(word) else \" \" for word in words])\n",
    "            res.append(tmp.rstrip())\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "\n",
    "        length = []\n",
    "\n",
    "        words =  s.split(' ')\n",
    "\n",
    "        for i in range(len(words)):\n",
    "            length.append(len(words[i]))\n",
    "        \n",
    "        max_len = max(length)\n",
    "\n",
    "        res = []\n",
    "        s = ''\n",
    "\n",
    "        for i in range(0, max_len):\n",
    "            for j in range(len(words)):\n",
    "                if i < len(words[j]):\n",
    "                    s += words[j][i]\n",
    "                else:\n",
    "                    s += ' '\n",
    "\n",
    "            k_cut = 0\n",
    "\n",
    "            for k in range(len(s)-1, 0, -1):\n",
    "                if s[k] != ' ':\n",
    "                    k_cut = k\n",
    "                    break\n",
    "            \n",
    "            s = s[0:k_cut+1]\n",
    "            \n",
    "            res.append(s)\n",
    "            s = ''\n",
    "        \n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split()\n",
    "        n = max([len(w) for w in words])\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            substr = \"\"\n",
    "            for word in words:\n",
    "                substr += word[i] if i < len(word) else \" \"\n",
    "            res.append(substr.rstrip())\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split(\" \")\n",
    "        longest = len(words[0])\n",
    "        for i in words:\n",
    "            if len(i) > longest:\n",
    "                longest = len(i)\n",
    "        res = []\n",
    "        for i in range(0, longest):\n",
    "            res.append(\"\")\n",
    "            for j in words:\n",
    "                if len(j) >= i + 1:\n",
    "                    res[-1] += j[i]\n",
    "                else:\n",
    "                    res[-1] += \" \"\n",
    "        \n",
    "        result = []\n",
    "        for i in res:\n",
    "            result.append(i.rstrip())\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s = s.split()\n",
    "        return [''.join(l).rstrip() for l in zip_longest(*s, fillvalue = ' ')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split()\n",
    "        maxlen = len(max(words, key=len))\n",
    "        ans = []\n",
    "        for i in range(maxlen):\n",
    "            concat = \"\".join([word[i] if i < len(word) else \" \" for word in words])\n",
    "            ans.append(concat.rstrip())\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "       words = s.split()\n",
    "       maxlen = max(len(word) for word in words)\n",
    "       ans = []\n",
    "       for i in range(maxlen):\n",
    "           concat = \"\".join([word[i]  if i<len(word) else \" \" for word in words])\n",
    "           ans.append(concat.rstrip())\n",
    "       return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words=s.split()\n",
    "        maxlen=max(len(word)for word in words)\n",
    "        ans=list()\n",
    "        for i in range(maxlen):\n",
    "            concat=\"\".join([word[i] if i <len(word) else\" \" for word in words])\n",
    "            ans.append(concat.rstrip())\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split(\" \")\n",
    "        n = max([len(x) for x in words])\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            a = \"\"\n",
    "            for w in words:\n",
    "                if i >= len(w):\n",
    "                    a += \" \"\n",
    "                else:\n",
    "                    a += w[i]\n",
    "            ans.append(a.rstrip())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s_list = s.split(\" \")\n",
    "        n = len(s)\n",
    "        max_length = 0\n",
    "        for word in s_list:\n",
    "            if len(word) > max_length:\n",
    "                max_length = len(word)\n",
    "        res = [\"\" for _ in range(max_length)]\n",
    "        count = 0\n",
    "        for c in s:\n",
    "            \n",
    "            if c == \" \":\n",
    "                for i in range(count, max_length):\n",
    "                    res[i] += \" \"\n",
    "                count = 0\n",
    "            else:\n",
    "                res[count] += c\n",
    "                count += 1\n",
    "        for i in range(len(res)):\n",
    "            res[i] = res[i].rstrip()\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s = s.split(' ')\n",
    "        max_length = 0\n",
    "        res = []\n",
    "        for i in range(0, len(s)):\n",
    "            max_length = max(max_length,len(s[i]))\n",
    "        for row in range(0, max_length):\n",
    "            temp = ''\n",
    "            for column in range(0,len(s)):\n",
    "                try:\n",
    "                    temp += s[column][row]\n",
    "                except IndexError:\n",
    "                    temp += ' '\n",
    "            res.append(temp.rstrip(' '))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s = s.split()\n",
    "        arr_len = [len(x) for x in s]\n",
    "        max_len = max(arr_len)\n",
    "        for i in range(len(s)):\n",
    "            if arr_len[i] < max_len:\n",
    "                s[i] += \" \" * (max_len - arr_len[i])\n",
    "        ans = []\n",
    "        for i in zip(*s):\n",
    "            ans.append(\"\".join(i).rstrip())\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s = s.split()\n",
    "        col = max(len(x) for x in s)\n",
    "        ans = [''] * col\n",
    "        for j in range(col):\n",
    "            for w in s:\n",
    "                if j < len(w):\n",
    "                    ans[j] += w[j]\n",
    "                else:\n",
    "                    ans[j] += ' '\n",
    "        return [x.rstrip(' ') for x in ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "\n",
    "        words = s.split(' ')\n",
    "        num_words = len(words)\n",
    "        mx_len = 0\n",
    "        for w in words:\n",
    "            mx_len = max(mx_len, len(w))\n",
    "        \n",
    "        res = [''] * mx_len\n",
    "\n",
    "        for w in words:\n",
    "            for i, ch in enumerate(w):\n",
    "                res[i] += ch\n",
    "            \n",
    "            for j in range(i+1, mx_len):\n",
    "                res[j] += ' '\n",
    "        \n",
    "        for idx in range(mx_len):\n",
    "            r = res[idx]\n",
    "            for i in range(num_words-1, -1, -1):\n",
    "                if r[i] != ' ':\n",
    "                    break\n",
    "            \n",
    "            res[idx] = res[idx][:i+1]\n",
    "        \n",
    "        return res\n",
    "            \n",
    "                    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s_list = s.split()\n",
    "        max_len = 0\n",
    "        for i in range(len(s_list)):\n",
    "            max_len = max(max_len,len(s_list[i]))\n",
    "\n",
    "        res = []\n",
    "        for i in range(max_len):\n",
    "            res.append(\"\")\n",
    "            for j in range(len(s_list)):\n",
    "                if i <len(s_list[j]):\n",
    "                    res[i]+=s_list[j][i]\n",
    "                else:\n",
    "                    res[i]+=\" \"\n",
    "            while res[i][-1] == \" \":\n",
    "                res[i] = res[i][:-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s_list = s.split(' ')\n",
    "        max_= max([ len(i) for i in s_list])\n",
    "        res = []\n",
    "        for i in range(max_):\n",
    "            s = ''\n",
    "            for str in s_list:\n",
    "                if  len(str) > i:\n",
    "                    s+=str[i]\n",
    "                else:\n",
    "                    s+=' '\n",
    "            res.append(s.rstrip())\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "            res=[]\n",
    "            datas=s.split( )\n",
    "            n=max( [max(len(word) for word in datas)])\n",
    "            i=0\n",
    "            while i<n:\n",
    "                cur=''\n",
    "                for data in datas:\n",
    "                    if len(data)-1>=i:\n",
    "                        cur+=data[i]\n",
    "                    else:\n",
    "                        cur+=' '\n",
    "                cur = cur.rstrip()\n",
    "                res.append(cur)\n",
    "                i+=1\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        def quwei(t):\n",
    "            index = len(t)-1\n",
    "            for i in range(len(t)-1,-1,-1):\n",
    "                if t[i] != ' ':\n",
    "                    index = i\n",
    "                    break\n",
    "            return t[:index+1]\n",
    "        max_ = 0\n",
    "        res = []\n",
    "        sl = s.split(' ')\n",
    "        for w in sl:\n",
    "            max_ = max(max_,len(w))\n",
    "        for i in range(max_):\n",
    "            tmp = ''\n",
    "            for w in sl:\n",
    "                if i < len(w):\n",
    "                    tmp += w[i]\n",
    "                else:\n",
    "                    tmp += ' '\n",
    "            res.append(quwei(tmp))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        ss=s.split(\" \")\n",
    "        maxx = 0\n",
    "        for x in ss:\n",
    "            maxx = max(maxx,len(x))\n",
    "        arr = [[] for _ in range(maxx)]\n",
    "        for i in range(maxx):\n",
    "            now = \"\"\n",
    "            for j in range(len(ss)):\n",
    "                if len(ss[j]) - 1 < i:\n",
    "                    now += \" \"\n",
    "                else:\n",
    "                    now += ss[j][i]\n",
    "            arr[i]=now.rstrip()\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        matrix = s.split()\n",
    "\n",
    "        col = max(map(len, matrix))\n",
    "        ans = []\n",
    "\n",
    "\n",
    "        for j in range(col):\n",
    "            string = ''.join(string[j] if j < len(string) else ' ' for string in matrix)\n",
    "            ans.append(string.rstrip())\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split()\n",
    "        maxlen = max(len(word) for word in words)\n",
    "        ans = []\n",
    "        for i in range(maxlen):\n",
    "            concat = \"\".join([word[i] if i < len(word) else \" \" for word in words])\n",
    "            ans.append(concat.rstrip())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "            res=[]\n",
    "            datas=s.split( )\n",
    "            n=max( [max(len(word) for word in datas)])\n",
    "            i=0\n",
    "            while i<n:\n",
    "                cur=''\n",
    "                for data in datas:\n",
    "                    if len(data)-1>=i:\n",
    "                        cur+=data[i]\n",
    "                    else:\n",
    "                        cur+=' '\n",
    "                cur = cur.rstrip()\n",
    "                res.append(cur)\n",
    "                i+=1\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split(' ')\n",
    "        mxl = 0\n",
    "        for word in words:\n",
    "            mxl = max(mxl, len(word)) \n",
    "        m = len(words)\n",
    "        for i in range(m):\n",
    "            words[i] += ' ' * (mxl - len(words[i])) \n",
    "        ans = []\n",
    "        for j in range(mxl):\n",
    "            tmp = ''\n",
    "            for i in range(m):\n",
    "                tmp += words[i][j] \n",
    "            ans.append(tmp.rstrip(' ')) \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        resList= [ [\" \"] * len(s.split(\" \")) for _ in range(max([len(_) for _ in s.split(\" \")])) ]\n",
    "        for word,i in zip(s.split(\" \"), range(len(s.split(\" \")))):\n",
    "            for letter, j in zip(word,range(len(word))):\n",
    "                resList[j][i] = letter\n",
    "        \n",
    "        res = []\n",
    "        \n",
    "        for _ in resList:\n",
    "            lineTmp = \"\".join(_)\n",
    "            while True:\n",
    "                if lineTmp[-1] == \" \":\n",
    "                    lineTmp = lineTmp[:-1]\n",
    "                else:\n",
    "                    break\n",
    "            res.append(lineTmp)\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        splits = s.strip().split(' ')\n",
    "        n = len(splits)\n",
    "        result = list()\n",
    "        j = 0\n",
    "        while True:\n",
    "            tmp = \"\"\n",
    "            is_break = True\n",
    "            for i in range(n):\n",
    "                if j < len(splits[i]):\n",
    "                    tmp += splits[i][j]\n",
    "                    is_break = False\n",
    "                else:\n",
    "                    tmp += \" \"\n",
    "            if is_break:\n",
    "                break\n",
    "            result.append(tmp.rstrip(\" \"))\n",
    "            j += 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # * 模拟\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split()\n",
    "        maxlen = max(len(word) for word in words)\n",
    "        ans = list()\n",
    "        for i in range(maxlen):\n",
    "            concat = \"\".join([word[i] if i < len(word) else \" \" for word in words])\n",
    "            ans.append(concat.rstrip())\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        spl = s.split(' ')\n",
    "        wordnum = len(spl)\n",
    "        length = len(max(spl,key=len))\n",
    "        for w in range(0, wordnum):\n",
    "            spl[w] = spl[w] + ' ' * (length - len(spl[w]))\n",
    "        result = []\n",
    "        for i in range(0,length):\n",
    "            line = ''\n",
    "            for word in spl:\n",
    "                line += word[i]\n",
    "            line = line.rstrip()\n",
    "            result.append(line)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split()\n",
    "        maxlen = max(len(word) for word in words)\n",
    "        ans = list()\n",
    "        for i in range(maxlen):\n",
    "            concat = \"\".join([word[i] if i < len(word) else \" \" for word in words])\n",
    "            ans.append(concat.rstrip())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        list1 = s.split(' ')\n",
    "        maxlen = max(len(word) for word in list1)\n",
    "        ans = list()\n",
    "        for i in range(maxlen):\n",
    "            concat = \"\".join([word[i] if i < len(word) else \" \" for word in list1])\n",
    "            ans.append(concat.rstrip())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        return [''.join(l).rstrip() for l in zip_longest(*s.split(), fillvalue = ' ')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        res=[]\n",
    "        konge=[0 for i in range(20)]\n",
    "        kon=0\n",
    "        s=s.split(' ')\n",
    "        for p in range(len(s)):\n",
    "            i=s[p]\n",
    "            temp=list(i)\n",
    "            for j in range(max(len(temp),len(res))):\n",
    "                if j>=len(res) and j<len(temp):\n",
    "                    kg=''\n",
    "                    if len(res)>0:\n",
    "                        for q in range(len(res[0])-1):\n",
    "                            kg+=' '\n",
    "                    res.append(kg+temp[j])\n",
    "                elif j<len(res) and j<len(temp):\n",
    "                    res[j]+=temp[j]\n",
    "                elif j<len(res) and j>=len(temp):\n",
    "                    if p==(len(s)-1):\n",
    "                        while res[j][-1]==' ':\n",
    "                            res[j]=res[j][:-1]\n",
    "                    else:\n",
    "                        res[j]+=' '\n",
    "            \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = list(s.split(' '))\n",
    "        x = max([len(words[i]) for i in range(len(words))])\n",
    "        res = [''] * x\n",
    "        i = 0\n",
    "        for c in s:\n",
    "            if c != ' ' :\n",
    "                res[i % x] += c\n",
    "                i += 1\n",
    "            elif c == ' ': \n",
    "                while i % x != 0:\n",
    "                    res[i % x] += ' '\n",
    "                    i += 1\n",
    "        for i in range(0, x):\n",
    "            res[i] = res[i].rstrip()\n",
    "        return res\n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words=s.split()\n",
    "        l=max([len(word) for word in words])\n",
    "        ans=[]\n",
    "        for i in range(l):\n",
    "            temp=[]\n",
    "            for word in words:\n",
    "                try:\n",
    "                    temp.append(word[i])\n",
    "                except:\n",
    "                    temp.append(' ')\n",
    "            ans.append(''.join(temp).rstrip())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        l1 = list(s.split(\" \"))\n",
    "        long = 0\n",
    "        l2 = []\n",
    "        for i in range (len(l1)):\n",
    "            if len(l1[i]) > long:\n",
    "                long = len(l1[i])\n",
    "        for i in range (long):\n",
    "            l2.append(\"\")\n",
    "        for i in range (len(l1)):\n",
    "            for k in range (long):\n",
    "                if k + 1 <= len(l1[i]):\n",
    "                    l2[k] += l1[i][k]\n",
    "                else:\n",
    "                    l2[k] += \" \"\n",
    "        for i in range (len(l2)):\n",
    "            x = l2[i][::-1]\n",
    "            count = 0\n",
    "            for k in range (len(x)):\n",
    "                if x[k] == \" \":\n",
    "                    count += 1\n",
    "                else:\n",
    "                    break\n",
    "            l2[i] = x[count:][::-1]\n",
    "        return l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        split_s = s.split()\n",
    "        max_n = max([len(_) for _ in split_s])\n",
    "        for i in range(len(split_s)):\n",
    "            split_s[i] += (max_n - len(split_s[i])) * \" \"\n",
    "        new_item = zip(*split_s)\n",
    "        ans = []\n",
    "        for item in new_item:\n",
    "            ans.append(''.join(item).rstrip())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        x = s.split(' ')\n",
    "        #print(x)\n",
    "        m,n = 0,len(x)\n",
    "        for word in x:\n",
    "            if len(word) > m:\n",
    "                m = len(word)\n",
    "        ans = [['']*n for _ in range(m)]\n",
    "        #print(ans)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if len(x[j]) > i:\n",
    "                    #print(x[j][i])\n",
    "                    ans[i][j] = x[j][i]\n",
    "                else:\n",
    "                    ans[i][j] = ' '\n",
    "            #print(ans[i])\n",
    "        #print(ans)\n",
    "        res = []\n",
    "        for i in range(m):\n",
    "            n = len(ans[i])\n",
    "            while ans[i][n-1] == ' ':\n",
    "                n -= 1\n",
    "            #string = ans[][:n]\n",
    "            res.append(''.join(ans[i][:n]))\n",
    "        #print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split()\n",
    "        maxlen = max(len(w) for w in words)\n",
    "        return [''.join([w[i] if len(w)>i else ' ' for w in words]).rstrip() for i in range(maxlen)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        slist = s.strip().split()\n",
    "        print(slist)\n",
    "        m = 0\n",
    "        for i in range(len(slist)):\n",
    "            slist[i].strip()\n",
    "            m = max(m,len(slist[i]))\n",
    "        n = len(slist)\n",
    "        print(m,n,slist)\n",
    "        ans = []\n",
    "        for i in range(m):\n",
    "            tmp = \"\"\n",
    "            for j in range(n):\n",
    "                if i<len(slist[j]):\n",
    "                    tmp += slist[j][i]\n",
    "                else:\n",
    "                    tmp += \" \"\n",
    "            ans.append(tmp.rstrip())\n",
    "        return ans\n",
    "\n",
    "        # for i in range(m):\n",
    "        #     ans[i].rstrip()\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        words = s.split()\n",
    "        maxLen = max(len(word) for word in words)\n",
    "        ans = []\n",
    "        for i in range(maxLen):\n",
    "            concat = \"\".join([word[i] if i < len(word) else \" \" for word in words])\n",
    "            ans.append(concat.rstrip())\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        result = []\n",
    "        s = s.split(' ')\n",
    "        max_len = max(len(i) for i in s)\n",
    "        for i in range(max_len):\n",
    "            tmp = ''\n",
    "            for j in s:\n",
    "                if len(j) <= i:\n",
    "                    tmp += ' '\n",
    "                else:\n",
    "                    tmp += j[i]\n",
    "            result.append(tmp)\n",
    "        result = [i.rstrip() for i in result]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        s = s.split(\" \")\n",
    "        max_len = max(len(i) for i in s)\n",
    "        result = []\n",
    "        for i in range(max_len):\n",
    "            tmp = \"\".join(j[i] if i < len(j) else \" \" for j in s)\n",
    "            result.append(tmp.rstrip())\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        slist = s.split(' ')\n",
    "        m = 0\n",
    "        for word in slist:\n",
    "            m = max(m, len(word))\n",
    "        n = len(slist)\n",
    "        result = [[' ']*n for _ in range(m)]\n",
    "        for j in range(n):\n",
    "            word = slist[j]\n",
    "            for i in range(len(word)):\n",
    "                result[i][j] = word[i]\n",
    "        result = [''.join(word).rstrip() for word in result]\n",
    "        return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def printVertically(self, s: str) -> List[str]:\n",
    "        lst = s.split()\n",
    "        maxlen = max(len(word) for word in lst)\n",
    "        res = []\n",
    "        for i in range(maxlen):\n",
    "            concat = \"\".join([word[i] if i < len(word) else \" \" for word in lst])\n",
    "            res.append(concat.rstrip())\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
